package com.lynchcms.cms.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lynchcms.cms.model.entity.CmsAdmin;
import com.lynchcms.cms.service.CmsAdminSev;

/**
 * 管理员控制层处理
 * 
 * @author Lynch
 */
@Scope("prototype")
@Controller("cmsAdminCol")
@RequestMapping(value = "/admin")
public class CmsAdminCol {
	
	private CmsAdminSev cmsAdminSev;
	
	/**
	 * 查询管理员列表
	 * 
	 * @param rows 每页显示条目
	 * @param page 当前第几页
	 * @return map 包含2个key-value(1.总记录数。2.当前页面的list)
	 */
	@RequestMapping(value = "/adminList.jspx", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> adminList(Integer rows, Integer page, CmsAdmin cmsAdmin) {
		
//		String testJson = new Gson().toJson(cmsAdminSev.pagerListTest(rows, page));
//		System.out.println(testJson);
		return cmsAdminSev.pagerListTest(rows, page, cmsAdmin);
	}
	
	/**
	 * 新增管理员
	 * 
	 * @param cmsAdmin 管理员对象
	 * @return map 包含1个key-value(操作是否成功)
	 */
	@RequestMapping(value = "/adminAdd.jspx", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> adminAdd(CmsAdmin cmsAdmin) {

		Map<String, String> map = new HashMap<String, String>();
		try {
			cmsAdmin.setEnabled(true);
			cmsAdmin.setAdminCreateTime(new Date());
			cmsAdminSev.save(cmsAdmin);
			map.put("message", "保存成功");
		} catch (Exception e) {
			map.put("message", "保存失败");
		}
		return map; 
	}
	
	/**
	 * 更新管理员
	 * 
	 * @param cmsAdmin 管理员对象
	 * @return map 包含1个key-value(操作是否成功)
	 */
	@RequestMapping(value = "/adminUpdate.jspx", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> adminUpdate(CmsAdmin cmsAdmin) {

		Map<String, String> map = new HashMap<String, String>();
		try {
			cmsAdmin.setEnabled(true);
			cmsAdmin.setAdminUpadteTime(new Date());
			cmsAdminSev.update(cmsAdmin);
			map.put("message", "修改成功");
		} catch (Exception e) {
			map.put("message", "修改失败");
		}
		return map;
	}
	
	/**
	 * 删除管理员
	 * 
	 * @param adminIds 管理员PK集合
	 * @return map 包含1个key-value(操作是否成功)
	 */
	@RequestMapping(value = "/adminDelete.jspx", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> adminDelete(@RequestParam("adminIds") List<Long> adminIds) {

		Map<String, String> map = new HashMap<String, String>();
		try {
			cmsAdminSev.deleteAllByPKs(adminIds);
			map.put("message", "删除成功");
		} catch (Exception e) {
			map.put("message", "删除失败");
		}
		return map;
	}
	
	/**
	 * 设置用户的角色信息
	 * 
	 * @return map 包含1个key-value(操作是否成功)
	 */
	@RequestMapping(value = "/adminRoleSet.jspx", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> adminRoleSet(Long adminId, @RequestParam("roleIds") List<Long> roleIds) {

		Map<String, String> map = new HashMap<String, String>();
		try {
			cmsAdminSev.updateAdminRole(adminId, roleIds);
			map.put("message", "修改成功");
		} catch (Exception e) {
			map.put("message", "修改失败");
		}
		return map;
	}
	
	public CmsAdminSev getCmsAdminSev() {
		return cmsAdminSev;
	}

	@Resource
	public void setCmsAdminSev(CmsAdminSev cmsAdminSev) {
		this.cmsAdminSev = cmsAdminSev;
	}
	
	
//	@RequestMapping(value = "/adminListUrl.jspx")
//	public String adminListUrl(int page, HttpServletRequest request) {
//		
//		Pager pager = cmsAdminSev.pagerList(Constants.DATANUMBER, page);
//		request.setAttribute("pager", pager);
//		return "adminListUrl";
//	}
//	
//	@RequestMapping(value = "/adminListForm.jspx")
//	public String adminListForm(Integer page, HttpServletRequest request) {
//		
//		if(page==null){
//			page = 0;
//		}
//		Pager pager = cmsAdminSev.pagerList(Constants.DATANUMBER, page);
//		request.setAttribute("pager", pager);
//		return "adminListForm";
//	}
	
	
}
